#include "Nano100Series.h"
#include "EventQueue.h"
#include "Common.h"
#include "string.h"

struct event_t hmd_events[HMD_EVENT_SIZE];
struct event_queue_t hmd_event_queue;

// tail point to empty element
// head point to non-empty element
void EventQueueInit(struct event_queue_t *event_queue, struct event_t *event, uint32_t size)
{
	event_queue->event = event;
	event_queue->size = size;
	event_queue->head = 0;
	event_queue->tail = 0;
}

static int EventQueueIsFull(struct event_queue_t *event_queue)
{
	if ((event_queue->tail + 1) % event_queue->size == event_queue->head) {
		return 1;
	} else {
		return 0;
	}
}

static int EventQueueIsEmpty(struct event_queue_t *event_queue)
{
	if (event_queue->head == event_queue->tail) {
		return 1;
	} else {
		return 0;
	}
}

int EventQueueEnQueue(struct event_queue_t *event_queue, const struct event_t *event)
{
	if (EventQueueIsFull(event_queue)) {
		return 1;
	} else {
		memcpy(event_queue->event + event_queue->tail, event, sizeof(*event));
		event_queue->tail = (event_queue->tail + 1) % event_queue->size;
		return 0;
	}
}

int EventQueueDeQueue(struct event_queue_t *event_queue, struct event_t *event)
{
	if (EventQueueIsEmpty(event_queue)) {
		return 1;
	} else {
		memcpy(event, event_queue->event + event_queue->head, sizeof(*event));
		event_queue->head = (event_queue->head + 1) % event_queue->size;
		return 0;
	}
}
